package com.lou1052.data.query.page;



import com.lou1052.core.base.Duad;
import com.lou1052.data.config.DataConfig;
import com.lou1052.data.exception.MorePageSizeException;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页查询条件
 * Created with IntelliJ IDEA.
 * User: liuqing
 * Date: 2016/1/1
 * Time: 19:58
 * To change this template use File | Settings | File Templates.
 */
public class Page implements IPage {

    public static  Page start(int pageNum,int pageSize){
        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        return page;
    }
    /**
     * 页数
     */
    private int pageNum;
    /**
     * 每页大小
     */
    private int pageSize;

    /**
     * 自动计算总数
     */
    private boolean autoCount = true;

    /**
     * 导航页码数
     */
    private int navigatePages = 8;

    /**
     * 排序条件
     */
    private final List<Duad<String, String>> orders = new ArrayList<>();

    /**
     * Getter for property 'pageNum'.
     *
     * @return Value for property 'pageNum'.
     */
    public int getPageNum() {
        return pageNum;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public int getNavigatePages() {
        return navigatePages;
    }

    @Override
    public boolean isAutoCount() {
        return autoCount;
    }

    @Override
    public List<Duad<String, String>> getOrders() {
        return orders;
    }

    /**
     * Setter for property 'pageNum'.
     *
     * @param pageNum Value to set for property 'pageNum'.
     */
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    @Override
    public void setNavigatePages(int navigatePages) {
        this.navigatePages=navigatePages;
    }

    /**
     * Setter for property 'pageSize'.
     *
     * @param pageSize Value to set for property 'pageSize'.
     */
    public final void setPageSize(int pageSize) {
        if(pageSize> DataConfig.MAX_PAGE_SIZE){
            throw new MorePageSizeException("query pageSize="+pageSize+" but MAX_PAGE_SIZE="+ DataConfig.MAX_PAGE_SIZE);
        }
        this.pageSize = pageSize;
    }

    @Override
    public void setAutoCount(boolean autoCount) {
        this.autoCount = autoCount;
    }

    /**
     * 兼容 curPage 参数
     * @return
     */
    public int getCurPage() {
        return getPageNum();
    }

    /**
     * 兼容 curPage 参数
     */
    public void setCurPage(int curPage) {
        setPageNum(curPage);
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", autoCount=" + autoCount +
                ", navigatePages=" + navigatePages +
                ", orders=" + orders +
                '}';
    }
}
